package by.sjc.grsu.web.controller;

import by.sjc.grsu.model.Periodical;
import by.sjc.grsu.service.PeriodicalsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class PeriodicalsController {
    public static final Logger logger = Logger.getLogger(PeriodicalsController.class);

    @Autowired
    private PeriodicalsService periodicalsService;

    @RequestMapping(value = "/list")
    public ModelAndView listPeriodicals() {
        List<Periodical> periodicals = periodicalsService.getAllPeriodicals();

        ModelAndView mav = new ModelAndView("periodical/list");
        mav.addObject("periodicals", periodicals);
        return mav;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addPeriodical(
            @RequestParam(value = "error", required = false) String error) {
        ModelAndView mav = new ModelAndView("periodical/add");
        mav.addObject("periodical", new Periodical());
        mav.addObject("error",error);
        return mav;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addPeriodical(Periodical periodical) {
        logger.info("periodicals="+periodical);
        ModelAndView mav = new ModelAndView();

        if (periodicalsService.addPeriodical(periodical)) {
            mav.setViewName("redirect:/list");
        } else {
            mav.setViewName("redirect:/add");
            mav.addObject("error", "error, mb already exist?");
        }
        return mav;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editPeriodical(
            @RequestParam("id") int id,
            @RequestParam(value = "error", required = false) String error) {
        ModelAndView mav = new ModelAndView("periodical/edit");
        Periodical periodical = periodicalsService.getById(id);
        mav.addObject("periodical", periodical);
        mav.addObject("error",error);
        return mav;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ModelAndView editPeriodical(Periodical periodical) {
        logger.info("periodicals="+periodical);
        ModelAndView mav = new ModelAndView();

        if (periodicalsService.editPeriodical(periodical)) {
            mav.setViewName("redirect:/list");
        } else {
            mav.setViewName("redirect:/edit");
            mav.addObject("id", periodical.getId());
            mav.addObject("error", "error, mb already exist?");
        }
        return mav;
    }


    @RequestMapping(value = "/delete")
    public ModelAndView deletePeriodicals(
            @RequestParam(value = "id") int periodicalId) {

        periodicalsService.deleteById(periodicalId);

        ModelAndView mav = new ModelAndView("redirect:/list");
        return mav;
    }
}
